package mails;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;

public class Tools {

	static ArrayList<Integer> rndmArray = new ArrayList<Integer>();
	static int sumFound=0;
	
	public static Integer countWords(String text) {

		String[] words = text.split("[\\s\\p{Punct}]+");
		return words.length;

	}

	public static String[] splitWords(String text) {

		String[] words = text.split("[\\s\\p{Punct}]+");
		return words;

	}

	/*
	 * Fills the array with numbers [0, the number of collected mails containing
	 * search term]
	 */

	public static ArrayList<Integer> initRandom(int range) {
		for (int i = 0; i < range; i++) {
			rndmArray.add(i);
		}
		return rndmArray;
	}

	public static Integer calcRandom() {

		/* Selects a random position out of array and delete it afterwards */

		int pos = ((int) (Math.random() * (rndmArray.size() - 1)));
		int rndm = rndmArray.get(pos);
		rndmArray.remove(pos);
		return rndm;
	}

	public static HashMap<String, Integer> wordIndex(String text) {
		HashMap<String, Integer> wordIndex = new HashMap<String, Integer>();
		String[] words = text.split("[\\s\\p{Punct}]+");

		for (int i = 0; i < words.length; i++) {
			String[] singleWord = text.split(words[i]);
			wordIndex.put(words[i], singleWord.length - 1);
		}
		return wordIndex;
	}

	public static HashMap<String, Integer> wordIndex2(String text,
			HashMap<String, Integer> wordIndex) {

		int c = 0;
		String recentWord;
		String[] words = text.split("[\\s\\p{Punct}]+");

		for (int i = 0; i < words.length; i++) {
			recentWord = words[i].toLowerCase();
			if (wordIndex.containsKey(recentWord)) {
				c = wordIndex.get(recentWord) + 1;
				wordIndex.put(recentWord, c);
			} else {
				wordIndex.put(recentWord, 1);
			}
		}
		return wordIndex;
	}

	public static ArrayList<String> HashToList(HashMap<String, Integer> hash) {

		ArrayList<String> sortedList = new ArrayList<String>();
		sortedList.addAll(hash.keySet());
		Collections.sort(sortedList);

		return sortedList;
	}

	public static String shorten(String shortStr, String suchwort) {

		int pos = shortStr.toLowerCase().indexOf(suchwort+" ");
		int start, ende;
			

		if (pos > 50) {
			start = pos - 51;
		} else {
			start = pos;
		}

		if (shortStr.length() < pos + 50) {
			ende = shortStr.length();
		} else {
			ende = pos + 50;
		}

		String substr = shortStr.substring(start, ende);
		substr = substr.replaceAll("\n", "<>");
		substr = substr.replaceAll("\r", "<>");
		substr = substr.replaceAll(">", "");
		String outStr = "[...] " + substr + " [...]";

		return outStr;
	}

	public static String[] pushStringToArray(String[] bodyArray,
			String insertStr) {

		if (!bodyArray[0].equals(insertStr)) {
			sumFound++;
			for (int i = bodyArray.length - 1; i > 0; i--) {
				bodyArray[i] = bodyArray[i - 1];
			}
			bodyArray[0] = insertStr;


		}
		return bodyArray;
	}

}
